package com.ruoyi.project.dataviewSystem.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Handler;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.dataviewSystem.domain.CDatavalue;
import com.ruoyi.project.dataviewSystem.service.ICDatavalueService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 历史数据Controller
 * 
 * @author wangshuo
 * @date 2021-01-07
 */
@RestController
@RequestMapping("/dataviewSystem/historicaldata")
public class CDatavalueController extends BaseController
{
    @Autowired
    private ICDatavalueService cDatavalueService;

    /**
     * 查询历史数据列表
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:historicaldata:list')")
    @GetMapping("/list")
    public TableDataInfo list(CDatavalue cDatavalue)
    {
        startPage();
        List<CDatavalue> list = cDatavalueService.selectCDatavalueList(cDatavalue);
        return getDataTable(list);
    }

    /**
     * 导出历史数据列表
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:historicaldata:export')")
    @Log(title = "历史数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CDatavalue cDatavalue)
    {
        List<CDatavalue> list = cDatavalueService.selectCDatavalueList(cDatavalue);
        ExcelUtil<CDatavalue> util = new ExcelUtil<CDatavalue>(CDatavalue.class);
        return util.exportExcel(list, "historicaldata");
    }

    /**
     * 获取历史数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:historicaldata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cDatavalueService.selectCDatavalueById(id));
    }

    /**
     * 新增历史数据
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:historicaldata:add')")
    @Log(title = "历史数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CDatavalue cDatavalue)
    {
        return toAjax(cDatavalueService.insertCDatavalue(cDatavalue));
    }

    /**
     * 修改历史数据
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:historicaldata:edit')")
    @Log(title = "历史数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CDatavalue cDatavalue)
    {
        return toAjax(cDatavalueService.updateCDatavalue(cDatavalue));
    }

    /**
     * 删除历史数据
     */
    @PreAuthorize("@ss.hasPermi('dataviewSystem:historicaldata:remove')")
    @Log(title = "历史数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cDatavalueService.deleteCDatavalueByIds(ids));
    }

    /**
     * 获取工艺类型下的指标当前数据
     */
    @GetMapping("/getLastDatavalue")
    public AjaxResult getLastDatavalue(String stationcode,String dataItemId)
    {
        return AjaxResult.success(cDatavalueService.getLastDatavalueByDataItemId(stationcode, dataItemId));
    }


    /**
     * 获取指标数据（当前，历史）
     */
    @GetMapping("/getDatavalueByDay")
    public AjaxResult getDatavalueByDay(String stationcode,String datacode,Integer day)
    {
        List<CDatavalue> datavalueByDay = cDatavalueService.getDatavalueByDay(stationcode, datacode, day);
        HashMap<String, Object> dataMap = new HashMap<>();
        SimpleDateFormat sf;
        if(day!=null&&day==1){
            sf = new SimpleDateFormat("HH:ss:mm");
        }else {
            sf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        }
//        Date[] dateArray = datavalueByDay.stream().map(CDatavalue::getDatadate).toArray(Date[]::new);
        String[] dateArray = datavalueByDay.stream().map(x -> sf.format(x.getDatadate())).toArray(String[]::new);
        BigDecimal[] valueArray = datavalueByDay.stream().map(CDatavalue::getNumvalue).toArray(BigDecimal[]::new);
        dataMap.put("dateArray",dateArray);
        dataMap.put("valueArray",valueArray);
//        dataMap.put("dataList",datavalueByDay);
        AjaxResult ajaxResult = AjaxResult.success();;
        ajaxResult.put("data",dataMap);
        return ajaxResult;
//        return AjaxResult.success(datavalueByDay);
    }

    @GetMapping("/getDatavalueList")
    public TableDataInfo getDatavalue(String stationcode, String datacode,Integer day) {
        startPage();
        List<CDatavalue> datavalueByDay = cDatavalueService.getDatavalueByDay(stationcode, datacode, day);
        return getDataTable(datavalueByDay);
    }
}
